package com.zfzs.post.page;

import com.google.common.collect.Lists;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 与具体ORM实现无关的分页参数及查询结果封装.
 *
 * @author: ck
 * @create: 2019/9/24/19:04
 **/
public class JpaPageHelper<T> {


    //-- 分页参数 --//
    protected int pageNo = 1;
    protected int pageSize = 15;
    protected int first = -1; //此处添加主要是为了方便分库分表时计算各表步进取值，当大于0时说明getFirst()函数不从pageNo计算获得
    protected boolean autoCount = true;
    protected String sort = "id";
    protected String dir = "ASC";
    //-- 返回结果 --//
    protected List result = Lists.newArrayList();
    protected long totalCount = -1;

    //-- 构造函数 --//
    public JpaPageHelper() {
    }

    public JpaPageHelper(List<Map<String, Object>> data, int size, int pageNo, int totalCount) {
        setPageNo(pageNo);
        setPageSize(size);
        setTotalCount(totalCount);
        setResult(data);

    }

    public JpaPageHelper(int pageSize) {
        this.pageSize = pageSize;
    }

    public void transferFromSqlList(Class clazz) {
        List tResult = Lists.newArrayList();
        ArrayList<Object> objArr;
        for (Object objItem : result) {
            try {
                objArr = new ArrayList<Object>(Arrays.asList((Object[]) objItem));
                Constructor<T> constructor = clazz.getDeclaredConstructor(objArr.getClass());
                tResult.add(constructor.newInstance(objArr));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.result = tResult;
    }

    public void copyPropertiesFromAnother(JpaPageHelper<?> page) {
        this.pageNo = page.getPageNo();
        this.pageSize = page.getPageSize();
        this.first = page.getFirst();
        this.autoCount = page.isAutoCount();
        this.sort = page.getSort();
        this.dir = page.getDir();
        this.totalCount = page.getTotalCount();
    }

    //-- 访问查询参数函数 --//

    /**
     * 获得当前页的页号,序号从1开始,默认为1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    public JpaPageHelper<T> pageNo(final int thePageNo) {
        setPageNo(thePageNo);
        return this;
    }

    /**
     * 获得每页的记录数量,默认为1.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页的记录数量,低于1时自动调整为1.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;

        if (pageSize < 1) {
            this.pageSize = 1;
        }
    }

    public JpaPageHelper<T> pageSize(final int thePageSize) {
        setPageSize(thePageSize);
        return this;
    }


    /**
     * @return the sort
     */
    public String getSort() {
        return sort;
    }

    /**
     * @param sort the sort to set
     */
    public void setSort(String sort) {
        this.sort = sort;
    }

    /**
     * @return the dir
     */
    public String getDir() {
        return dir;
    }

    /**
     * @param dir the dir to set
     */
    public void setDir(String dir) {
        this.dir = dir;
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public int getFirst() {
        if (this.first > 0)
            return first;
        return ((pageNo - 1) * pageSize) + 1;
    }


    /**
     * @param first the first to set
     */
    public void setFirst(int first) {
        this.first = first;
    }

    /**
     * 查询对象时是否自动另外执行count查询获取总记录数, 默认为false.
     */
    public boolean isAutoCount() {
        return autoCount;
    }

    /**
     * 查询对象时是否自动另外执行count查询获取总记录数.
     */
    public void setAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }

    public JpaPageHelper<T> autoCount(final boolean theAutoCount) {
        setAutoCount(theAutoCount);
        return this;
    }

    //-- 访问查询结果函数 --//

    /**
     * 取得页内的记录列表.
     */
    public List getResult() {
        return result;
    }

    /**
     * 设置页内的记录列表.
     */
    public void setResult(final List result) {
        this.result = result;
    }

    /**
     * 取得总记录数, 默认值为-1.
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * 设置总记录数.
     */
    public void setTotalCount(final long totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 根据pageSize与totalCount计算总页数, 默认值为-1.
     */
    public long getTotalPages() {
        if (totalCount < 0) {
            return -1;
        }

        long count = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return (pageNo + 1 <= getTotalPages());
    }

    /**
     * 取得下页的页号, 序号从1开始.
     * 当前页为尾页时仍返回尾页序号.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return pageNo + 1;
        } else {
            return pageNo;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return (pageNo - 1 >= 1);
    }

    /**
     * 取得上页的页号, 序号从1开始.
     * 当前页为首页时返回首页序号.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return pageNo - 1;
        } else {
            return pageNo;
        }
    }
}
